package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 分类管理业务层
 */
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public void save(CategoryDTO categoryDTO) {
        Category category = new Category();
        // 1.拷贝属性
        BeanUtils.copyProperties(categoryDTO, category);

        // 2.补全属性
        category.setStatus(StatusConstant.DISABLE);     // 默认禁用
//        category.setCreateTime(LocalDateTime.now());
//        category.setUpdateTime(LocalDateTime.now());
//        category.setCreateUser(BaseContext.getCurrentId());
//        category.setUpdateUser(BaseContext.getCurrentId());

        // 3.调用 mapper 保存
        categoryMapper.insert(category);
    }

    @Override
    public PageResult pageQuery(CategoryPageQueryDTO categoryPageQueryDTO) {
        // 1.设置分页参数
        PageHelper.startPage(categoryPageQueryDTO.getPage(), categoryPageQueryDTO.getPageSize());

        // 2.调用 mapper 查询数据（模糊查询），封装
        List<Category> categoryList = categoryMapper.list(categoryPageQueryDTO);

        // 3.解析结果
        Page<Category> page = (Page<Category>) categoryList;

        // 4.返回
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public void editCategory(CategoryDTO categoryDTO) {
        // 1.拷贝属性
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);

        // 2.补全属性
//        category.setUpdateTime(LocalDateTime.now());
//        category.setUpdateUser(BaseContext.getCurrentId());

        // 3.调用 mapper 动态更新
        categoryMapper.update(category);
    }

    @Override
    public void delCategory(Long id) {
        // 1.查询该分类是否有关联菜品若有则不能删除
        Integer dishCount = dishMapper.countByCategoryId(id);
        if(dishCount > 0) {
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }

        // 2.查询该分类是否有关联套餐若有则不能删除
        Integer mealCount = setmealMapper.countByCategoryId(id);
        if(mealCount > 0) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        // 3.调用 mapper 删除分类
        categoryMapper.deleteById(id);
    }

    @Override
    public void enableOrDisable(Integer status, Long id) {
        Category category = Category.builder()
                .id(id)
                .status(status)
//                .updateTime(LocalDateTime.now())
//                .updateUser(BaseContext.getCurrentId())
                .build();
        categoryMapper.update(category);
    }

    @Override
    public List<Category> list(Integer type) {
        List<Category> categoryList = categoryMapper.getByType(type);
        return categoryList;
    }

}
